﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument
{
    /// <summary>
    /// onlineperson 的摘要说明。
    /// </summary>
    public partial class OnlinePersonPage : Page
    {
        //存放页眉文字
        private string[] headtext;

        protected static TimeSpan GetOnlineTime(DateTime logintime, DateTime activetime)
        {
            return (activetime - logintime);
        }

        protected override void OnInit(EventArgs e)
        {
            dgrd_OnlinePerson.SortCommand += dgrd_OnlinePerson_SortCommand;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                headtext = new String[dgrd_OnlinePerson.Columns.Count];
                for (int i = 0; i < dgrd_OnlinePerson.Columns.Count; i++)
                {
                    headtext[i] = dgrd_OnlinePerson.Columns[i].HeaderText;
                }
                ViewState["headtext"] = headtext;
                ViewState["SortField"] = "Position_Name";
                ViewState["SortDirect"] = "ASC";
                Bind();
            }
            else
            {
                //把页眉复位
                headtext = (string[])ViewState["headtext"];
                for (int i = 0; i < dgrd_OnlinePerson.Columns.Count; i++)
                {
                    dgrd_OnlinePerson.Columns[i].HeaderText = headtext[i];
                }
            }

            base.OnLoad(e);
        }

        private void Bind()
        {
            var sms = new SMS();
            DataTable dt;
            using (SqlDataReader dr = sms.GetOnlinePerson())
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }

            dt.DefaultView.Sort = ViewState["SortField"] + " " + ViewState["SortDirect"];
            dgrd_OnlinePerson.DataSource = dt.DefaultView;
            dgrd_OnlinePerson.DataBind();
        }

        private void dgrd_OnlinePerson_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (ViewState["SortField"].ToString() == e.SortExpression)
            {
                ViewState["SortDirect"] = (ViewState["SortDirect"].ToString() == "ASC") ? "DESC" : "ASC";
            }
            else
            {
                ViewState["SortField"] = e.SortExpression;
                ViewState["SortDirect"] = "ASC";
            }

            foreach (DataGridColumn col in dgrd_OnlinePerson.Columns)
            {
                if (col.SortExpression == ViewState["SortField"].ToString())
                {
                    if (ViewState["SortDirect"].ToString() == "ASC")
                        col.HeaderText += "<img src='../../images/asc.gif' border=0/>";
                    else
                        col.HeaderText += "<img src='../../images/desc.gif' border=0/>";
                }
            }

            Bind();
        }
    }
}